<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>rect_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_rect_test.js.html">rect_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.math.RectTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.math.RectTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line19"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line20"></a>goog.require(&#39;goog.math.Rect&#39;);
<a name="line21"></a>goog.require(&#39;goog.math.Size&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Produce legible assertion results. If two rects are not equal, the error
<a name="line27"></a> * message will be of the form
<a name="line28"></a> * &quot;Expected &lt;(1, 2 - 10 x 10)&gt; (Object) but was &lt;(3, 4 - 20 x 20)&gt; (Object)&quot;
<a name="line29"></a> */
<a name="line30"></a>function assertRectsEqual(expected, actual) {
<a name="line31"></a>  if (!goog.math.Rect.equals(expected, actual)) {
<a name="line32"></a>    assertEquals(expected, actual);
<a name="line33"></a>  }
<a name="line34"></a>}
<a name="line35"></a>
<a name="line36"></a>function createRect(a) {
<a name="line37"></a>  return a ? new goog.math.Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null;
<a name="line38"></a>}
<a name="line39"></a>
<a name="line40"></a>function testRectClone() {
<a name="line41"></a>  var r = new goog.math.Rect(0, 0, 0, 0);
<a name="line42"></a>  assertRectsEqual(r, r.clone());
<a name="line43"></a>  r.left = -10;
<a name="line44"></a>  r.top = -20;
<a name="line45"></a>  r.width = 10;
<a name="line46"></a>  r.height = 20;
<a name="line47"></a>  assertRectsEqual(r, r.clone());
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>function testRectIntersection() {
<a name="line51"></a>  var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]],
<a name="line52"></a>               [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]],
<a name="line53"></a>               [[0, 0, 1, 1], [10, 11, 12, 13], null],
<a name="line54"></a>               [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]];
<a name="line55"></a>  for (var i = 0; i &lt; tests.length; ++i) {
<a name="line56"></a>    var t = tests[i];
<a name="line57"></a>    var r0 = createRect(t[0]);
<a name="line58"></a>    var r1 = createRect(t[1]);
<a name="line59"></a>
<a name="line60"></a>    var expected = createRect(t[2]);
<a name="line61"></a>
<a name="line62"></a>    assertRectsEqual(expected, goog.math.Rect.intersection(r0, r1));
<a name="line63"></a>    assertRectsEqual(expected, goog.math.Rect.intersection(r1, r0));
<a name="line64"></a>
<a name="line65"></a>    // Test in place methods.
<a name="line66"></a>    var clone = r0.clone();
<a name="line67"></a>
<a name="line68"></a>    assertRectsEqual(expected, clone.intersection(r1) ? clone : null);
<a name="line69"></a>    assertRectsEqual(expected, r1.intersection(r0) ? r1 : null);
<a name="line70"></a>  }
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testRectIntersects() {
<a name="line74"></a>  var r0 = createRect([10, 10, 20, 20]);
<a name="line75"></a>  var r1 = createRect([15, 15, 25, 25]);
<a name="line76"></a>  var r2 = createRect([0, 0, 1, 1]);
<a name="line77"></a>
<a name="line78"></a>  assertTrue(goog.math.Rect.intersects(r0, r1));
<a name="line79"></a>  assertTrue(goog.math.Rect.intersects(r1, r0));
<a name="line80"></a>  assertTrue(r0.intersects(r1));
<a name="line81"></a>  assertTrue(r1.intersects(r0));
<a name="line82"></a>
<a name="line83"></a>  assertFalse(goog.math.Rect.intersects(r0, r2));
<a name="line84"></a>  assertFalse(goog.math.Rect.intersects(r2, r0));
<a name="line85"></a>  assertFalse(r0.intersects(r2));
<a name="line86"></a>  assertFalse(r2.intersects(r0));
<a name="line87"></a>}
<a name="line88"></a>
<a name="line89"></a>function testRectBoundingRect() {
<a name="line90"></a>  var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]],
<a name="line91"></a>               [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]],
<a name="line92"></a>               [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]],
<a name="line93"></a>               [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]];
<a name="line94"></a>  for (var i = 0; i &lt; tests.length; ++i) {
<a name="line95"></a>    var t = tests[i];
<a name="line96"></a>    var r0 = createRect(t[0]);
<a name="line97"></a>    var r1 = createRect(t[1]);
<a name="line98"></a>    var expected = createRect(t[2]);
<a name="line99"></a>    assertRectsEqual(expected, goog.math.Rect.boundingRect(r0, r1));
<a name="line100"></a>    assertRectsEqual(expected, goog.math.Rect.boundingRect(r1, r0));
<a name="line101"></a>
<a name="line102"></a>    // Test in place methods.
<a name="line103"></a>    var clone = r0.clone();
<a name="line104"></a>
<a name="line105"></a>    clone.boundingRect(r1);
<a name="line106"></a>    assertRectsEqual(expected, clone);
<a name="line107"></a>
<a name="line108"></a>    r1.boundingRect(r0);
<a name="line109"></a>    assertRectsEqual(expected, r1);
<a name="line110"></a>  }
<a name="line111"></a>}
<a name="line112"></a>
<a name="line113"></a>function testRectDifference() {
<a name="line114"></a>  // B is the same as A.
<a name="line115"></a>  assertDifference([10, 10, 20, 20], [10, 10, 20, 20], []);
<a name="line116"></a>  // B does not touch A.
<a name="line117"></a>  assertDifference([10, 10, 20, 20], [0, 0, 5, 5], [[10, 10, 20, 20]]);
<a name="line118"></a>  // B overlaps top half of A.
<a name="line119"></a>  assertDifference([10, 10, 20, 20], [5, 15, 25, 25],
<a name="line120"></a>      [[10, 10, 20, 15]]);
<a name="line121"></a>  // B overlaps bottom half of A.
<a name="line122"></a>  assertDifference([10, 10, 20, 20], [5, 5, 25, 15],
<a name="line123"></a>      [[10, 15, 20, 20]]);
<a name="line124"></a>  // B overlaps right half of A.
<a name="line125"></a>  assertDifference([10, 10, 20, 20], [15, 5, 25, 25],
<a name="line126"></a>      [[10, 10, 15, 20]]);
<a name="line127"></a>  // B overlaps left half of A.
<a name="line128"></a>  assertDifference([10, 10, 20, 20], [5, 5, 15, 25],
<a name="line129"></a>      [[15, 10, 20, 20]]);
<a name="line130"></a>  // B touches A at its bottom right corner
<a name="line131"></a>  assertDifference([10, 10, 20, 20], [20, 20, 30, 30],
<a name="line132"></a>      [[10, 10, 20, 20]]);
<a name="line133"></a>  // B touches A at its top left corner
<a name="line134"></a>  assertDifference([10, 10, 20, 20], [5, 5, 10, 10],
<a name="line135"></a>      [[10, 10, 20, 20]]);
<a name="line136"></a>  // B touches A along its bottom edge
<a name="line137"></a>  assertDifference([10, 10, 20, 20], [12, 20, 17, 25],
<a name="line138"></a>      [[10, 10, 20, 20]]);
<a name="line139"></a>  // B splits A horizontally.
<a name="line140"></a>  assertDifference([10, 10, 20, 20], [5, 12, 25, 18],
<a name="line141"></a>      [[10, 10, 20, 12], [10, 18, 20, 20]]);
<a name="line142"></a>  // B splits A vertically.
<a name="line143"></a>  assertDifference([10, 10, 20, 20], [12, 5, 18, 25],
<a name="line144"></a>      [[10, 10, 12, 20], [18, 10, 20, 20]]);
<a name="line145"></a>  // B subtracts a notch from the top of A.
<a name="line146"></a>  assertDifference([10, 10, 20, 20], [12, 5, 18, 15],
<a name="line147"></a>      [[10, 15, 20, 20], [10, 10, 12, 15], [18, 10, 20, 15]]);
<a name="line148"></a>  // B subtracts a notch from the bottom left of A
<a name="line149"></a>  assertDifference([1, 6, 3, 9], [1, 7, 2, 9],
<a name="line150"></a>      [[1, 6, 3, 7], [2, 7, 3, 9]]);
<a name="line151"></a>  // B subtracts a notch from the bottom right of A
<a name="line152"></a>  assertDifference([1, 6, 3, 9], [2, 7, 3, 9],
<a name="line153"></a>      [[1, 6, 3, 7], [1, 7, 2, 9]]);
<a name="line154"></a>  // B subtracts a notch from the top left of A
<a name="line155"></a>  assertDifference([1, 6, 3, 9], [1, 6, 2, 8],
<a name="line156"></a>      [[1, 8, 3, 9], [2, 6, 3, 8]]);
<a name="line157"></a>  // B subtracts a notch from the top left of A (no coinciding edge)
<a name="line158"></a>  assertDifference([1, 6, 3, 9], [0, 5, 2, 8],
<a name="line159"></a>      [[1, 8, 3, 9], [2, 6, 3, 8]]);
<a name="line160"></a>  // B subtracts a hole from the center of A.
<a name="line161"></a>  assertDifference([-20, -20, -10, -10], [-18, -18, -12, -12],
<a name="line162"></a>      [[-20, -20, -10, -18], [-20, -12, -10, -10],
<a name="line163"></a>       [-20, -18, -18, -12], [-12, -18, -10, -12]]);
<a name="line164"></a>}
<a name="line165"></a>
<a name="line166"></a>function assertDifference(a, b, expected) {
<a name="line167"></a>  var r0 = createRect(a);
<a name="line168"></a>  var r1 = createRect(b);
<a name="line169"></a>  var diff = goog.math.Rect.difference(r0, r1);
<a name="line170"></a>
<a name="line171"></a>  assertEquals(&#39;Wrong number of rectangles in difference &#39;,
<a name="line172"></a>      expected.length, diff.length);
<a name="line173"></a>
<a name="line174"></a>  for (var j = 0; j &lt; expected.length; ++j) {
<a name="line175"></a>    var e = createRect(expected[j]);
<a name="line176"></a>    if (!goog.math.Rect.equals(e, diff[j])) {
<a name="line177"></a>      alert(j + &#39;: &#39; + e + &#39; != &#39; + diff[j]);
<a name="line178"></a>    }
<a name="line179"></a>    assertRectsEqual(e, diff[j]);
<a name="line180"></a>  }
<a name="line181"></a>
<a name="line182"></a>  // Test in place version
<a name="line183"></a>  var diff = r0.difference(r1);
<a name="line184"></a>
<a name="line185"></a>  assertEquals(&#39;Wrong number of rectangles in in-place difference &#39;,
<a name="line186"></a>      expected.length, diff.length);
<a name="line187"></a>
<a name="line188"></a>  for (var j = 0; j &lt; expected.length; ++j) {
<a name="line189"></a>    var e = createRect(expected[j]);
<a name="line190"></a>    if (!goog.math.Rect.equals(e, diff[j])) {
<a name="line191"></a>      alert(j + &#39;: &#39; + e + &#39; != &#39; + diff[j]);
<a name="line192"></a>    }
<a name="line193"></a>    assertRectsEqual(e, diff[j]);
<a name="line194"></a>  }
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>function testRectToBox() {
<a name="line198"></a>  var r = new goog.math.Rect(0, 0, 0, 0);
<a name="line199"></a>  assertObjectEquals(new goog.math.Box(0, 0, 0, 0), r.toBox());
<a name="line200"></a>
<a name="line201"></a>  r.top = 10;
<a name="line202"></a>  r.left = 10;
<a name="line203"></a>  r.width = 20;
<a name="line204"></a>  r.height = 20;
<a name="line205"></a>  assertObjectEquals(new goog.math.Box(10, 30, 30, 10), r.toBox());
<a name="line206"></a>
<a name="line207"></a>  r.top = -10;
<a name="line208"></a>  r.left = 0;
<a name="line209"></a>  r.width = 10;
<a name="line210"></a>  r.height = 10;
<a name="line211"></a>  assertObjectEquals(new goog.math.Box(-10, 10, 0, 0), r.toBox());
<a name="line212"></a>}
<a name="line213"></a>
<a name="line214"></a>function testBoxToRect() {
<a name="line215"></a>  var box = new goog.math.Box(0, 0, 0, 0);
<a name="line216"></a>  assertObjectEquals(new goog.math.Rect(0, 0, 0, 0),
<a name="line217"></a>                     goog.math.Rect.createFromBox(box));
<a name="line218"></a>
<a name="line219"></a>  box.top = 10;
<a name="line220"></a>  box.left = 15;
<a name="line221"></a>  box.right = 23;
<a name="line222"></a>  box.bottom = 27;
<a name="line223"></a>  assertObjectEquals(new goog.math.Rect(15, 10, 8, 17),
<a name="line224"></a>                     goog.math.Rect.createFromBox(box));
<a name="line225"></a>
<a name="line226"></a>  box.top = -10;
<a name="line227"></a>  box.left = 3;
<a name="line228"></a>  box.right = 12;
<a name="line229"></a>  box.bottom = 7;
<a name="line230"></a>  assertObjectEquals(new goog.math.Rect(3, -10, 9, 17),
<a name="line231"></a>                     goog.math.Rect.createFromBox(box));
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>function testBoxToRectAndBack() {
<a name="line235"></a>  rectToBoxAndBackTest(new goog.math.Rect(8, 11, 20, 23));
<a name="line236"></a>  rectToBoxAndBackTest(new goog.math.Rect(9, 13, NaN, NaN));
<a name="line237"></a>  rectToBoxAndBackTest(new goog.math.Rect(10, 13, NaN, 21));
<a name="line238"></a>  rectToBoxAndBackTest(new goog.math.Rect(5, 7, 14, NaN));
<a name="line239"></a>}
<a name="line240"></a>
<a name="line241"></a>function rectToBoxAndBackTest(rect) {
<a name="line242"></a>  var box = rect.toBox();
<a name="line243"></a>  var rect2 = goog.math.Rect.createFromBox(box);
<a name="line244"></a>
<a name="line245"></a>  // Use toString for this test since otherwise NaN != NaN.
<a name="line246"></a>  assertObjectEquals(rect.toString(), rect2.toString());
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>function testRectToBoxAndBack() {
<a name="line250"></a>  // This doesn&#39;t work if left or top is undefined.
<a name="line251"></a>  boxToRectAndBackTest(new goog.math.Box(11, 13, 20, 17));
<a name="line252"></a>  boxToRectAndBackTest(new goog.math.Box(10, NaN, NaN, 11));
<a name="line253"></a>  boxToRectAndBackTest(new goog.math.Box(9, 14, NaN, 11));
<a name="line254"></a>  boxToRectAndBackTest(new goog.math.Box(10, NaN, 22, 15));
<a name="line255"></a>}
<a name="line256"></a>
<a name="line257"></a>function boxToRectAndBackTest(box) {
<a name="line258"></a>  var rect = goog.math.Rect.createFromBox(box);
<a name="line259"></a>  var box2 = rect.toBox();
<a name="line260"></a>
<a name="line261"></a>  // Use toString for this test since otherwise NaN != NaN.
<a name="line262"></a>  assertEquals(box.toString(), box2.toString());
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>function testRectContainsRect() {
<a name="line266"></a>  var r = new goog.math.Rect(-10, 0, 20, 10);
<a name="line267"></a>  assertTrue(r.contains(r));
<a name="line268"></a>  assertFalse(r.contains(new goog.math.Rect(NaN, NaN, NaN, NaN)));
<a name="line269"></a>  var r2 = new goog.math.Rect(0, 2, 5, 5);
<a name="line270"></a>  assertTrue(r.contains(r2));
<a name="line271"></a>  assertFalse(r2.contains(r));
<a name="line272"></a>  r2.left = -11;
<a name="line273"></a>  assertFalse(r.contains(r2));
<a name="line274"></a>  r2.left = 0;
<a name="line275"></a>  r2.width = 15;
<a name="line276"></a>  assertFalse(r.contains(r2));
<a name="line277"></a>  r2.width = 5;
<a name="line278"></a>  r2.height = 10;
<a name="line279"></a>  assertFalse(r.contains(r2));
<a name="line280"></a>  r2.top = 0;
<a name="line281"></a>  assertTrue(r.contains(r2));
<a name="line282"></a>}
<a name="line283"></a>
<a name="line284"></a>function testRectContainsCoordinate() {
<a name="line285"></a>  var r = new goog.math.Rect(20, 40, 60, 80);
<a name="line286"></a>
<a name="line287"></a>  // Test middle.
<a name="line288"></a>  assertTrue(r.contains(new goog.math.Coordinate(50, 80)));
<a name="line289"></a>
<a name="line290"></a>  // Test edges.
<a name="line291"></a>  assertTrue(r.contains(new goog.math.Coordinate(20, 40)));
<a name="line292"></a>  assertTrue(r.contains(new goog.math.Coordinate(50, 40)));
<a name="line293"></a>  assertTrue(r.contains(new goog.math.Coordinate(80, 40)));
<a name="line294"></a>  assertTrue(r.contains(new goog.math.Coordinate(80, 80)));
<a name="line295"></a>  assertTrue(r.contains(new goog.math.Coordinate(80, 120)));
<a name="line296"></a>  assertTrue(r.contains(new goog.math.Coordinate(50, 120)));
<a name="line297"></a>  assertTrue(r.contains(new goog.math.Coordinate(20, 120)));
<a name="line298"></a>  assertTrue(r.contains(new goog.math.Coordinate(20, 80)));
<a name="line299"></a>
<a name="line300"></a>  // Test outside.
<a name="line301"></a>  assertFalse(r.contains(new goog.math.Coordinate(0, 0)));
<a name="line302"></a>  assertFalse(r.contains(new goog.math.Coordinate(50, 0)));
<a name="line303"></a>  assertFalse(r.contains(new goog.math.Coordinate(100, 0)));
<a name="line304"></a>  assertFalse(r.contains(new goog.math.Coordinate(100, 80)));
<a name="line305"></a>  assertFalse(r.contains(new goog.math.Coordinate(100, 160)));
<a name="line306"></a>  assertFalse(r.contains(new goog.math.Coordinate(50, 160)));
<a name="line307"></a>  assertFalse(r.contains(new goog.math.Coordinate(0, 160)));
<a name="line308"></a>  assertFalse(r.contains(new goog.math.Coordinate(0, 80)));
<a name="line309"></a>}
<a name="line310"></a>
<a name="line311"></a>function testGetSize() {
<a name="line312"></a>  assertObjectEquals(new goog.math.Size(60, 80),
<a name="line313"></a>                     new goog.math.Rect(20, 40, 60, 80).getSize());
<a name="line314"></a>}
<a name="line315"></a>
<a name="line316"></a>function testGetBottomRight() {
<a name="line317"></a>  assertObjectEquals(new goog.math.Coordinate(40, 60),
<a name="line318"></a>                     new goog.math.Rect(10, 20, 30, 40).getBottomRight());
<a name="line319"></a>}
<a name="line320"></a>
<a name="line321"></a>function testGetCenter() {
<a name="line322"></a>  assertObjectEquals(new goog.math.Coordinate(25, 40),
<a name="line323"></a>                     new goog.math.Rect(10, 20, 30, 40).getCenter());
<a name="line324"></a>}
<a name="line325"></a>
<a name="line326"></a>function testGetTopLeft() {
<a name="line327"></a>  assertObjectEquals(new goog.math.Coordinate(10, 20),
<a name="line328"></a>                     new goog.math.Rect(10, 20, 30, 40).getTopLeft());
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>function testRectCeil() {
<a name="line332"></a>  var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
<a name="line333"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line334"></a>      rect, rect.ceil());
<a name="line335"></a>  assertRectsEqual(new goog.math.Rect(12, 27, 18, 10), rect);
<a name="line336"></a>}
<a name="line337"></a>
<a name="line338"></a>function testRectFloor() {
<a name="line339"></a>  var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
<a name="line340"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line341"></a>      rect, rect.floor());
<a name="line342"></a>  assertRectsEqual(new goog.math.Rect(11, 26, 17, 9), rect);
<a name="line343"></a>}
<a name="line344"></a>
<a name="line345"></a>function testRectRound() {
<a name="line346"></a>  var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
<a name="line347"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line348"></a>      rect, rect.round());
<a name="line349"></a>  assertRectsEqual(new goog.math.Rect(11, 27, 18, 9), rect);
<a name="line350"></a>}
<a name="line351"></a>
<a name="line352"></a>function testRectTranslateCoordinate() {
<a name="line353"></a>  var rect = new goog.math.Rect(10, 40, 30, 20);
<a name="line354"></a>  var c = new goog.math.Coordinate(10, 5);
<a name="line355"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line356"></a>      rect, rect.translate(c));
<a name="line357"></a>  assertRectsEqual(new goog.math.Rect(20, 45, 30, 20), rect);
<a name="line358"></a>}
<a name="line359"></a>
<a name="line360"></a>function testRectTranslateXY() {
<a name="line361"></a>  var rect = new goog.math.Rect(10, 20, 40, 35);
<a name="line362"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line363"></a>      rect, rect.translate(15, 10));
<a name="line364"></a>  assertRectsEqual(new goog.math.Rect(25, 30, 40, 35), rect);
<a name="line365"></a>}
<a name="line366"></a>
<a name="line367"></a>function testRectTranslateX() {
<a name="line368"></a>  var rect = new goog.math.Rect(12, 34, 113, 88);
<a name="line369"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line370"></a>      rect, rect.translate(10));
<a name="line371"></a>  assertRectsEqual(new goog.math.Rect(22, 34, 113, 88), rect);
<a name="line372"></a>}
<a name="line373"></a>
<a name="line374"></a>function testRectScaleXY() {
<a name="line375"></a>  var rect = new goog.math.Rect(10, 30, 100, 60);
<a name="line376"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line377"></a>      rect, rect.scale(2, 5));
<a name="line378"></a>  assertRectsEqual(new goog.math.Rect(20, 150, 200, 300), rect);
<a name="line379"></a>}
<a name="line380"></a>
<a name="line381"></a>function testRectScaleFactor() {
<a name="line382"></a>  var rect = new goog.math.Rect(12, 34, 113, 88);
<a name="line383"></a>  assertEquals(&#39;The function should return the target instance&#39;,
<a name="line384"></a>      rect, rect.scale(10));
<a name="line385"></a>  assertRectsEqual(new goog.math.Rect(120, 340, 1130, 880), rect);
<a name="line386"></a>}
<a name="line387"></a>
<a name="line388"></a>function testSquaredDistance() {
<a name="line389"></a>  var rect = new goog.math.Rect(-10, -20, 15, 25);
<a name="line390"></a>
<a name="line391"></a>  // Test regions:
<a name="line392"></a>  // 1  2  3
<a name="line393"></a>  //   +-+
<a name="line394"></a>  // 4 |5| 6
<a name="line395"></a>  //   +-+
<a name="line396"></a>  // 7  8  9
<a name="line397"></a>
<a name="line398"></a>  // Region 5 (inside the rectangle).
<a name="line399"></a>  assertEquals(0, rect.squaredDistance(new goog.math.Coordinate(-10, 5)));
<a name="line400"></a>  assertEquals(0, rect.squaredDistance(new goog.math.Coordinate(5, -20)));
<a name="line401"></a>
<a name="line402"></a>  // 1, 2, and 3.
<a name="line403"></a>  assertEquals(25, rect.squaredDistance(new goog.math.Coordinate(9, 8)));
<a name="line404"></a>  assertEquals(36, rect.squaredDistance(new goog.math.Coordinate(2, 11)));
<a name="line405"></a>  assertEquals(53, rect.squaredDistance(new goog.math.Coordinate(12, 7)));
<a name="line406"></a>
<a name="line407"></a>  // 4 and 6.
<a name="line408"></a>  assertEquals(81, rect.squaredDistance(new goog.math.Coordinate(-19, -10)));
<a name="line409"></a>  assertEquals(64, rect.squaredDistance(new goog.math.Coordinate(13, 0)));
<a name="line410"></a>
<a name="line411"></a>  // 7, 8, and 9.
<a name="line412"></a>  assertEquals(20, rect.squaredDistance(new goog.math.Coordinate(-12, -24)));
<a name="line413"></a>  assertEquals(9, rect.squaredDistance(new goog.math.Coordinate(0, -23)));
<a name="line414"></a>  assertEquals(34, rect.squaredDistance(new goog.math.Coordinate(8, -25)));
<a name="line415"></a>}
<a name="line416"></a>
<a name="line417"></a>
<a name="line418"></a>function testDistance() {
<a name="line419"></a>  var rect = new goog.math.Rect(2, 4, 8, 16);
<a name="line420"></a>
<a name="line421"></a>  // Region 5 (inside the rectangle).
<a name="line422"></a>  assertEquals(0, rect.distance(new goog.math.Coordinate(2, 4)));
<a name="line423"></a>  assertEquals(0, rect.distance(new goog.math.Coordinate(10, 20)));
<a name="line424"></a>
<a name="line425"></a>  // 1, 2, and 3.
<a name="line426"></a>  assertRoughlyEquals(
<a name="line427"></a>      Math.sqrt(8), rect.distance(new goog.math.Coordinate(0, 22)), .0001);
<a name="line428"></a>  assertEquals(8, rect.distance(new goog.math.Coordinate(9, 28)));
<a name="line429"></a>  assertRoughlyEquals(
<a name="line430"></a>      Math.sqrt(50), rect.distance(new goog.math.Coordinate(15, 25)), .0001);
<a name="line431"></a>
<a name="line432"></a>  // 4 and 6.
<a name="line433"></a>  assertEquals(7, rect.distance(new goog.math.Coordinate(-5, 6)));
<a name="line434"></a>  assertEquals(10, rect.distance(new goog.math.Coordinate(20, 10)));
<a name="line435"></a>
<a name="line436"></a>  // 7, 8, and 9.
<a name="line437"></a>  assertEquals(5, rect.distance(new goog.math.Coordinate(-2, 1)));
<a name="line438"></a>  assertEquals(2, rect.distance(new goog.math.Coordinate(5, 2)));
<a name="line439"></a>  assertRoughlyEquals(
<a name="line440"></a>      Math.sqrt(10), rect.distance(new goog.math.Coordinate(1, 1)), .0001);
<a name="line441"></a>}
</pre>


</body>
</html>
